//
//  ZZNetWorkRequest.m
//  ZZNetWorker
//
//  Created by ZengLiZhi on 2024/1/30.
//

#import "ZZNetWorkRequest.h"
#import "ZZNetWorker.h"

@interface ZZNetWorkRequest ()

@property (nonatomic, strong) NSURLSessionDataTask *_task;
@end

@implementation ZZNetWorkRequest

- (void)dealloc{
    ZZLog(@"%@-dealloc",NSStringFromClass([self class]));
}

+ (ZZNetWorkRequest *)request{
    ZZNetWorkRequest *request = [[[self class] alloc] init];
    return request;
}

- (NSString *)url{
    
    NSAssert(self.api || self.baseUrl, @"baseUrl/api is null");
    
    if ([self.api hasPrefix:@"http"]) {
        return self.api;
    }
    if ([self.baseUrl hasSuffix:@"/"] && [self.api hasPrefix:@"/"]) {
        NSString *base = [self.baseUrl substringToIndex:self.baseUrl.length-1];
        return [base stringByAppendingString:self.api];
    }else if(![self.baseUrl hasSuffix:@"/"] && ![self.api hasPrefix:@"/"]){
        return [NSString stringWithFormat:@"%@/%@",self.baseUrl,self.api];
    }
    return [self.baseUrl stringByAppendingString:self.api];
}

- (ZZNetWorkResponse *)response{
    ZZNetWorkResponse *res = [[[self.responseClass class] alloc] init];
    res.dataClass = self.dataClass;
    res.testData = self.testData;
    return res;
}

#pragma mark - setter
- (ZZNetWorkRequest *(^)(NSString *))zz_url{
    return ^id (NSString *url){
        self.api = url;
        return self;
    };
}

- (ZZNetWorkRequest *(^)(NSString *))zz_signApi{
    return ^id (NSString *signApi){
        self.signApi = signApi;
        return self;
    };
}

- (ZZNetWorkRequest *(^)(NSString *))zz_authorization{
    return ^id (NSString *authorization){
        self.authorization = authorization;
        return self;
    };
}

- (ZZNetWorkRequest *(^)(NSString *))zz_baseUrl{
    return ^id (NSString *baseUrl){
        self.baseUrl = baseUrl;
        return self;
    };
}

- (ZZNetWorkRequest *(^)(id))zz_param{
    return ^id (id param){
        self.param = param;
        return self;
    };
}

- (ZZNetWorkRequest * (^)(id))zz_testResponse{
    return ^id (id data){
        self.testData = data;
        return self;
    };
}

- (ZZNetWorkRequest *(^)(BOOL))zz_log{
    return ^id (BOOL isOpenLog){
        self.isOpenLog = isOpenLog;
        return self;
    };
}

- (ZZNetWorkRequest *(^)(BOOL))zz_isShowErrorMessage{
    return ^id (BOOL isShowErrorMessage){
        self.isShowErrorMessage = isShowErrorMessage;
        return self;
    };
}

- (ZZNetWorkRequest *(^)(BOOL))zz_willHandlerParam{
    return ^id (BOOL willHandlerParam){
        self.willHandlerParam = willHandlerParam;
        return self;
    };
}

- (ZZNetWorkRequest * _Nonnull (^)(Class _Nonnull __unsafe_unretained))zz_responseClass{
    return ^id (Class class){
        NSAssert([class isKindOfClass:[ZZNetWorkResponse class]], @"responseClass 必须继承 ZZNetWorkResponse");
        self.responseClass = class;
        return self;
    };
}

- (ZZNetWorkRequest * _Nonnull (^)(Class class))zz_dataClass{
    return ^id (Class class){
        self.dataClass = class;
        return self;
    };
}

- (ZZNetWorkRequest * _Nonnull (^)(ZZNetWorkerProgressBlock))zz_progress{
    return ^id (ZZNetWorkerProgressBlock block){
        self.progress = block;
        return self;
    };
}

- (ZZNetWorkRequest *(^)(ZZNetWorkerCompletionBlock))zz_completion{
    return ^id (ZZNetWorkerCompletionBlock block){
        self.completion = block;
        NSURLSessionDataTask *task = [[ZZNetWorker woker] request:self];
        self._task = task;
        return self;
    };
}

-(ZZNetWorkRequest * _Nonnull (^)(ZZNetWorkerDidReceiveDataBlock _Nonnull))zz_receiveData{
    return ^id (ZZNetWorkerDidReceiveDataBlock block){
        self.receiveData = block;
        return self;
    };
}

- (NSURLSessionDataTask *)task{
    return __task;
}


- (ZZNetWorkRequest * _Nonnull (^)(AFHTTPRequestSerializer<AFURLRequestSerialization> * _Nonnull))zz_requestSerializer{
    return ^id (AFHTTPRequestSerializer<AFURLRequestSerialization> *requestSerializer){
        self.requestSerializer = requestSerializer;
        return self;
    };
}

- (ZZNetWorkRequest * _Nonnull (^)(AFHTTPResponseSerializer<AFURLResponseSerialization> * _Nonnull))zz_responseSerializer{
    return ^id (AFHTTPResponseSerializer<AFURLResponseSerialization> *responseSerializer){
        self.responseSerializer = responseSerializer;
        return self;
    };
}

- (ZZNetWorkRequest *(^)(BOOL))zz_isCancelSameRequest{
    return ^id (BOOL b){
        self.isCancelSameRequest = b;
        return self;
    };
}

- (ZZNetWorkRequest * _Nonnull (^)(NSDictionary<NSString *,NSString *> * _Nonnull))zz_headers{
    return ^id (NSDictionary<NSString *,NSString *> * headers){
        self.headers = headers;
        return self;
    };
}
@end
